from typing import Optional

from fastapi import Request

from auth.user.model import User


async def current_user(request: Request) -> Optional[User]:
    if request.user.is_authenticated:
        doc = await User.find_one(User.username == request.user.username)
        if doc:
            return doc
    return None
